<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="userId">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员卡名称" prop="memberCradId">
        <el-input
          v-model="queryParams.memberCradName"
          placeholder="请输入会员卡名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员名称" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入会员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员编码" prop="memberCode">
        <el-input
          v-model="queryParams.memberCode"
          placeholder="请输入会员编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="绑定时间" prop="memberTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.memberTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择绑定时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:member:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:member:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:member:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:member:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="客户名称" align="center" prop="userName" />
      <el-table-column label="会员卡名称" align="center" prop="memberCradName" />
      <el-table-column label="会员名称" align="center" prop="memberName" />
      <el-table-column label="会员编码" align="center" prop="memberCode" />
      <el-table-column label="绑定时间" align="center" prop="memberTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.memberTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:member:view']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:member:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:member:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="选择客户" prop="userId">
          <template>
            <el-select v-model="form.userId" filterable placeholder="请选择">
              <el-option
                v-for="item in userList"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="选择会员卡" prop="memberCradId">
          <template>
            <el-select v-model="form.memberCradId" filterable placeholder="请选择">
              <el-option
                v-for="item in memberCradList"
                :key="item.id"
                :label="item.memberCardName"
                :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="会员名称" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入会员名称" />
        </el-form-item>
        <el-form-item label="会员编码" prop="memberCode">
          <el-input v-model="form.memberCode" placeholder="请输入会员编码" />
        </el-form-item>
        <el-form-item label="绑定时间" prop="memberTime">
          <el-date-picker clearable
            v-model="form.memberTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择绑定时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="viewOpen"  append-to-body>
      <el-descriptions >
        <el-descriptions-item label="客户名称">{{ viewForm.userName }}</el-descriptions-item>
        <el-descriptions-item label="会员名称">{{ viewForm.memberName }}</el-descriptions-item>
        <el-descriptions-item label="会员卡名称">{{ viewForm.memberCradName }}</el-descriptions-item>
        <el-descriptions-item label="会员编码">{{ viewForm.memberCode }}</el-descriptions-item>
        <el-descriptions-item label="绑定时间">{{ parseTime(viewForm.memberTime, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="权益1剩余">{{ viewForm.equity1 ==null? "-":viewForm.equity1 +" 次"}} </el-descriptions-item>
        <el-descriptions-item label="权益2剩余">{{ viewForm.equity2 ==null? "-":viewForm.equity2 +" 次"}} </el-descriptions-item>
        <el-descriptions-item label="权益3剩余">{{ viewForm.equity3 ==null? "-":viewForm.equity3 +" 次"}}</el-descriptions-item>
        <el-descriptions-item label="权益4剩余">{{ viewForm.equity4 ==null? "-":viewForm.equity4 +" 次"}}</el-descriptions-item>
        <el-descriptions-item label="权益5剩余">{{ viewForm.equity5 ==null? "-":viewForm.equity5 +" 次"}}</el-descriptions-item>
        <el-descriptions-item label="权益6剩余">{{ viewForm.equity6 ==null? "-":viewForm.equity6 +" 次"}}</el-descriptions-item>
        <el-descriptions-item label="权益7剩余">{{ viewForm.equity7 ==null? "-":viewForm.equity7 +" 次"}}</el-descriptions-item>
        <el-descriptions-item label="权益8剩余">{{ viewForm.equity8 ==null? "-":viewForm.equity8 +" 次"}}</el-descriptions-item>
        <el-descriptions-item label="权益9剩余">{{ viewForm.equity9 ==null? "-":viewForm.equity9 +" 次"}}</el-descriptions-item>
        <el-descriptions-item label="权益10剩余">{{ viewForm.equity10 ==null? "-":viewForm.equity10 +" 次"}}</el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            权益说明
          </template>
          "-"代表该项无权益
          <br />
          权益1：专属心理匹配测试及剖析解读次数
          <br />
          权益2：高端猎婚专业婚恋情感分析次数
          <br />
          权益3：整体形象造型设计指导次数
          <br />
          权益4：建立专属爱情资料数据库，面对面优化征婚方案
          <br />
          权益5：资深婚恋老师全程跟踪、指导，筛选、安排一对一约见次数
          <br />
          权益6：提供公司内部干净舒适的约会环境次数
          <br />
          权益7：专享线上平台（钻石会员）优先展示特权和追求权限，置顶 次数
          <br />
          权益8：富顺城恋公众号发布个人征婚信息次数
          <br />
          权益9：红娘朋友圈发布个人征婚信息次数
          <br />
          权益10：参加内部和政企单位相亲活动次数，并享受优先获得合适异性料和推荐的 VIP 权限
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember,getMemberView } from "@/api/business/member";
import { getCardList} from "@/api/business/card";
import { getInfoList} from "@/api/business/info";

export default {
  name: "Member",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员管理表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      viewOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        memberCradName: null,
        memberName: null,
        memberCode: null
      },
      // 表单参数
      form: {},
      viewForm: {},
      // 表单校验
      rules: {
      },
      memberCradList:[],
      userList:[]
    };
  },
  created() {
    this.getList();


  },
  methods: {
    /** 查询会员管理列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        console.log("列表：",response);
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        memberCradId: null,
        memberCradName: null,
        memberName: null,
        memberCode: null,
        memberTime: null,
        memberType: null,
        operatorId: null,
        operatorName: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员管理";
      this.getMemberCradList();
      this.getUserList();
    },
    /** 详情 **/
    handleView(row){
      const id = row.id
      getMemberView(id).then(response => {
        this.viewForm = response.data;
        this.viewOpen = true;
        this.title = "会员详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getMemberCradList();
      this.getUserList();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log("form:",this.form);
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员管理编号为"' + ids + '"的数据项？').then(function() {
        return delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    },
    getMemberCradList(){
      getCardList().then(response => {
        console.log("参数："+response);
        this.memberCradList = response.data;
        console.log("参数后：", this.memberCradList);
      });
    },
    getUserList(){
      getInfoList().then(response => {
        this.userList = response.data;
      });
    }

  }
};
</script>
